package com.bckj.fastboot.core.sql.meta;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@ToString
public class Table implements TableLike {
    private final String alias;
    private final String tableName;
    private final List<Column> columns = new ArrayList<>();
    private Class<?> entityClass;
    private TableInfo tableInfo;

    public Table(String tableName, String alias) {
        this.tableName = tableName;
        this.alias = alias;
    }

    public static Table of(Class<?> entityClass, String alias) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
        String tableName = tableInfo.getTableName();
        Table table = new Table(tableName, alias);
        table.setEntityClass(entityClass);
        table.setTableInfo(tableInfo);
        return table;
    }

    public static Table of(TableLike tableLike, String alias) {
        return new Table(tableLike.getTableName(), alias);
    }
}
